package org.vacoor.nothing.misc.express.fedex;

import com.fedex.track.stub.*;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;

/**
 * FedEx 物流追踪
 *
 * @author vacoor
 */
public class FedExTrackService extends FedExSupport {

    public void track() {
        TrackIdentifierType type = TrackIdentifierType.TRACKING_NUMBER_OR_DOORTAG;
        String id = /*"123456789012";*/"865697180281";

        TrackRequest req = new TrackRequest();

        // 授权, 客户端, 协议版本信息
        req.setWebAuthenticationDetail(new WebAuthenticationDetail(new WebAuthenticationCredential(getKey(), getPassword())));
        req.setClientDetail(new ClientDetail(getAccountNumber(), getMeterNumber(), getIntegratorId(), null));
        req.setTransactionDetail(new TransactionDetail("Track Request", null));
        req.setVersion(new VersionId("trck", 9, 0, 0));

        // 创建追踪包裹对象
        TrackSelectionDetail tsd = new TrackSelectionDetail();
        tsd.setPackageIdentifier(new TrackPackageIdentifier(type, id));

        req.setSelectionDetails(new TrackSelectionDetail[]{tsd});

        // 追踪请求处理类型 (包含签收和扫描)
        TrackRequestProcessingOptionType processingOption = TrackRequestProcessingOptionType.INCLUDE_DETAILED_SCANS;
        req.setProcessingOptions(new TrackRequestProcessingOptionType[]{processingOption});

        try {
            TrackServiceLocator service = new TrackServiceLocator();
            TrackPortType port = service.getTrackServicePort();

            TrackReply reply = port.track(req);
            if (isResponseOk(reply.getHighestSeverity())) {

                TransactionDetail td = reply.getTransactionDetail();
                CompletedTrackDetail[] trackDetails = reply.getCompletedTrackDetails();
                for (CompletedTrackDetail detail : trackDetails) {
                    Boolean duplicateWaybill = detail.getDuplicateWaybill();
                    System.out.println(duplicateWaybill);
                    Boolean moreData = detail.getMoreData();
                    System.out.println(detail.getTrackDetailsCount());
                    System.out.println(detail.getPagingToken());


//                FedExHelper.printTrackDetail(detail.getTrackDetails());


                    FedExHelper.printNotifications(detail.getNotifications());
                }
                System.out.println("-------");
                FedExHelper.printCompletedTrackDetail(trackDetails);
            }
            FedExHelper.printNotifications(reply.getNotifications());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /* ***********************
     *      Helper Method
     * ***********************/
    private static boolean isResponseOk(NotificationSeverityType notificationSeverityType) {
        if (notificationSeverityType == null) {
            return false;
        }

        if (notificationSeverityType.equals(NotificationSeverityType.WARNING) ||
                notificationSeverityType.equals(NotificationSeverityType.NOTE) ||
                notificationSeverityType.equals(NotificationSeverityType.SUCCESS)) {
            return true;
        }
        return false;
    }
}
